package org.dromara.demo.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.TestDemo3;
import org.dromara.demo.domain.TestDemo3Dept;
import org.dromara.demo.domain.TestDemo3Partner;
import org.dromara.demo.domain.bo.TestDemo3Bo;
import org.dromara.demo.domain.vo.TestDemo3Vo;
import org.dromara.demo.mapper.TestDemo3DeptMapper;
import org.dromara.demo.mapper.TestDemo3Mapper;
import org.dromara.demo.mapper.TestDemo3PartnerMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 测试关联Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class TestDemo3ServiceImpl extends BaseService {

  private final TestDemo3Mapper baseMapper;

  private final TestDemo3PartnerMapper demo3PartnerMapper;

  private final TestDemo3DeptMapper demo3DeptMapper;

  /**
   * 通过id查询
   */
  public TestDemo3Vo queryById(Long id) {
    TestDemo3Vo testDemo3Vo = baseMapper.selectVoById(id);
    List<Long> partnerIds = demo3PartnerMapper.findByid(testDemo3Vo.getId());
    List<Long> deptIds = demo3DeptMapper.findByid(testDemo3Vo.getId());
    testDemo3Vo.setPartnerIds(partnerIds);
    testDemo3Vo.setDeptIds(deptIds);
    return testDemo3Vo;
  }

  /**
   * 查询测试列表
   */
  public TableDataInfo<TestDemo3Vo> queryPageList(TestDemo3Bo bo, PageQuery pageQuery) {
    LambdaQueryWrapper<TestDemo3> lqw = buildQueryWrapper(bo);
    Page<TestDemo3Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
    List<TestDemo3Vo> records = result.getRecords();
    for (TestDemo3Vo record : records) {
      Long demoId = record.getId();
      List<Long> partnerIds = demo3PartnerMapper.findByid(demoId);
      List<Long> deptIds = demo3DeptMapper.findByid(demoId);
      if(ArrayUtil.isNotEmpty(partnerIds)){
        record.setPartnerIds(partnerIds);
      }
      if(ArrayUtil.isNotEmpty(deptIds)){
        record.setDeptIds(deptIds);
      }
    }
    return TableDataInfo.build(result);
  }

  /**
   * 查询测试列表
   */
  public List<TestDemo3Vo> queryList(TestDemo3Bo bo) {
    LambdaQueryWrapper<TestDemo3> lqw = buildQueryWrapper(bo);
    return baseMapper.selectVoList(lqw);
  }

  private LambdaQueryWrapper<TestDemo3> buildQueryWrapper(TestDemo3Bo bo) {
    Map<String, Object> params = bo.getParams();
    LambdaQueryWrapper<TestDemo3> lqw = Wrappers.lambdaQuery();
    lqw.like(StringUtils.isNotBlank(bo.getName()), TestDemo3::getName, bo.getName());
    lqw.eq(StringUtils.isNotBlank(bo.getRemark()), TestDemo3::getRemark, bo.getRemark());
    lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TestDemo3::getStatus, bo.getStatus());
    return lqw;
  }

  /**
   * 新增测试
   */
  @Transactional(propagation = Propagation.NESTED)
  public Boolean insertByBo(TestDemo3Bo bo) {
    TestDemo3 add = MapstructUtils.convert(bo, TestDemo3.class);
    validEntityBeforeSave(add);
    return baseMapper.insert(add) > 0;
  }

  /**
   * 修改测试
   */
  @Transactional(propagation = Propagation.NESTED)
  public Boolean updateByBo(TestDemo3Bo bo) {
    List<Long> partnerIds = bo.getPartnerIds();
    List<Long> deptIds = bo.getDeptIds();
    TestDemo3 update = MapstructUtils.convert(bo, TestDemo3.class);
    validEntityBeforeSave(update);
    int flag = baseMapper.updateById(update);
    if (ArrayUtil.isEmpty(deptIds) && ArrayUtil.isEmpty(partnerIds)) {
      return flag > 0;
    }

    if(ArrayUtil.isNotEmpty(partnerIds)){
      demo3PartnerMapper.deleteBatchIds(Arrays.asList(update.getId()));
      TestDemo3Partner testDemo3Partner = new TestDemo3Partner();
      for (Long partnerId : partnerIds) {
        testDemo3Partner.setDemoId(update.getId());
        testDemo3Partner.setPartnerId(partnerId);
        demo3PartnerMapper.insert(testDemo3Partner);
      }
    }

    if(ArrayUtil.isNotEmpty(deptIds)){
      demo3DeptMapper.deleteBatchIds(Arrays.asList(update.getId()));
      TestDemo3Dept testDemo3Dept = new TestDemo3Dept();
      for (Long deptId : deptIds) {
        testDemo3Dept.setDemoId(update.getId());
        testDemo3Dept.setDeptId(deptId);
        demo3DeptMapper.insert(testDemo3Dept);
      }
    }

    return flag > 0;
  }

  /**
   * 保存前的数据校验
   */
  private void validEntityBeforeSave(TestDemo3 entity) {
    //TODO 做一些数据校验,如唯一约束
  }

  /**
   * 批量删除测试
   */
  @Transactional(propagation = Propagation.NESTED)
  public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
    if (isValid) {
      //TODO 做一些业务上的校验,判断是否需要校验
    }
    demo3DeptMapper.deleteBatchIds(ids);
    demo3PartnerMapper.deleteBatchIds(ids);
    return baseMapper.deleteBatchIds(ids) > 0;
  }
}
